<?Lassoscript
// Last modified 12/21/09 by Eric Landmann

// FUNCTIONALITY
// This establishes the queries and results pages for the admin Users area

// CHANGE NOTES
// 11/30/09
// First implementation
// 12/21/09
// Fix small problem with query building

If: !(Var_Defined:'SQLQueryUser'); Session_AddVariable: -Name='forums_admin', 'SQLQueryUser'; /If;

// Setting Variables for Nav Control
Var:'vNew' = (Action_Param:'New');
Var:'Records_Skipped' = (Action_Param:'skip');
Var:'This_Group' = (Action_Param:'group');
Var:'Records_Per_Page' = 10;
Var:'SkipCount' = integer;
Var:'Records_Found' = integer;
Var:'Group_Size' = (Math_Mult:(Var:'Records_Per_Page'),5);

// Debugging
Var:'svDebug' = 'Y';

// Output debugging filename
If: $svDebug == 'Y';
	'<br>\n';
	'<strong>user_searchresults.inc</strong><br>\n';
/If;

// If a new query, initialize all the variables
If: Var:'vNew' == 'Y';
	Var:'vFirst_Name' = (Action_Param:'First_Name');
	Var:'vLast_Name' = (Action_Param:'Last_Name');
	Var:'vPost_Name' = (Action_Param:'Post_Name');
	Var:'vMail' = (Action_Param:'Mail');
	Var:'SQL_User' = string;
	Var:'SQLQueryUser' = '';
	Var:'svSearchParams' = '';
	Var:'svSearchParamsURL' = '';

	// Include the code for building the search params phrase
	Include:($svLibsPath) 'searchparamsusers_build.inc';

/If;

If: Var:'SQLQueryUser' == '';
	Var:'SQLQueryUser' = '/* Admin - Select Member by Postname */
SELECT ID, First_Name, Last_Name, Mail, Entry_Date, Allow,
Post_Name1, Post_Name2, Post_Name3, Post_Name4, Post_Name5
FROM ' $svMembersTable '\n';

	If: (($vFirst_Name != '') || ($vLast_Name != '') || ($vPost_Name != '') || ($vMail != ''));
	
		$SQLQueryUser += 'WHERE ';
	
		If: (Var:'vLast_Name') != '';
			$SQLQueryUser += 'Last_Name LIKE "%'+($vLast_Name)+'%"\n';
		/If;
	
		If: (Var:'vFirst_Name') != '';
			If: (Var:'vLast_Name') != '';
				$SQLQueryUser += 'AND ';
			/If;
			$SQLQueryUser += 'First_Name LIKE "%'+($vFirst_Name)+'%"\n';
		/If;

		If: (Var:'vPost_Name') != '';
			If: ((Var:'vFirst_Name') != '') || ((Var:'vLast_Name') != '');
				$SQLQueryUser += 'AND ';
			/If;
			$SQLQueryUser += '(Post_Name1 LIKE "%'+($vPost_Name)+'%"\n';
			$SQLQueryUser += 'OR Post_Name2 LIKE "%'+($vPost_Name)+'%"\n';
			$SQLQueryUser += 'OR Post_Name3 LIKE "%'+($vPost_Name)+'%"\n';
			$SQLQueryUser += 'OR Post_Name4 LIKE "%'+($vPost_Name)+'%"\n';
			$SQLQueryUser += 'OR Post_Name5 LIKE "%'+($vPost_Name)+'%" )\n';
		/If;
	
		If: (Var:'vMail') != '';
			If: ((Var:'vFirst_Name') != '') || ((Var:'vLast_Name') != '') || ((Var:'vPost_Name') != '');
				$SQLQueryUser += 'AND ';
			/If;
			$SQLQueryUser += 'Mail LIKE "%'+($vMail)+'%"\n';
		/If;
	
	/If;

	$SQLQueryUser += 'ORDER BY Last_Name, First_Name';

	Debug;
		'53: vFirst_Name = ' (Var:'vFirst_Name') '<br>\n';
		'53: vLast_Name = ' (Var:'vLast_Name') '<br>\n';
		'53: vPost_Name = ' (Var:'vPost_Name') '<br>\n';
		'53: vMail = ' (Var:'vMail') '<br>\n';
		'53: SQLQueryUser = ' (Var:'SQLQueryUser') '<br>\n';
		'53: svSearchParams = ' (Var:'svSearchParams') '<br>\n';
		'53: svSearchParamsURL = ' (Var:'svSearchParamsURL') '<br>\n';
	/Debug;

/If;

Inline: $IV_SearchMembers,
	-SkipRecords=(Var:'Records_Skipped'),
	-MaxRecords=(Var:'Records_Per_Page'),
	-SQL=$SQLQueryUser;

	// Build the Nav Control Links
	Include:($svLibsPath) 'nav_control_admin.inc';

	Debug;
		'67: Error = ' (Error_CurrentError) '<br>\n';
		'67: Found_Count = ' (Found_Count) '<br>\n';
	/Debug;

	// If no records found, immediate redirect with error to setup_edit.lasso
	If: (Found_Count) == 0;
		Var:'vError'='2000';
		Var:'URL' = ('setup_edit.lasso?Error='$vError'&'(Client_POSTArgs));
		If: $svDebug == 'Y';
			Debug;
				'76: URL = ' $URL '<br>\n';
				'<a href="'$URL'">' $URL '</a><br>\n';
			/Debug;
		Else;
			Redirect_URL: $URL;
		/If;
	/If;

	// If only one record, redirect immediately to setup_editrecord.lasso
	// This shortcut skips past the search results page if there is only one record found
	// Example URL:
	// http://127.0.0.1/admin/setup_editrecord.lasso?DataType=User&ID=2&New=Y
	If: (Found_Count) == 1;
		Var:'vID'=(Field:'ID');
		Var:'URL' = ('setup_editrecord.lasso?DataType=User&ID='($vID)'&New=Y');
		If: $svDebug == 'Y';
			Debug;
				'96: URL = ' $URL '<br>\n';
				'<a href="'$URL'">' $URL '</a><br>\n';
			/Debug;
		Else;
			Redirect_URL: $URL;
		/If;
	/If;
?>
<div align="center">
<table border="0" cellpadding="0" cellspacing="0" width="525" bgcolor="black">
	<tr>
		<td width="620" valign="top" bgcolor="#FFFFFF">
			<div align="left">
				<h2>User Search Results</h2>
				<p><font color="red">[Var:'svSearchParams', -EncodeNone]</font>&nbsp;&nbsp;&nbsp;<a href="[$svSiteFolderRoot]/admin/setup_edit.lasso[$svSearchParamsURL]">Search Again</a></p>
			</div>
			<div align="left">
				<table width="535" cellspacing="0" cellpadding="5">
					<tr bgcolor="#000000">
						<td valign="middle" width="170" class="tablehead_10_white">User Name</td>
						<td valign="middle" width="200" class="tablehead_10_white">E-mail</td>
						<td valign="middle" width="120" class="tablehead_10_white">PostNames</td>
						<td valign="middle" width="80" class="tablehead_10_white">Entry Date</td>
						<td valign="middle" width="30" class="tablehead_10_white">Allow</td>
					</tr>
<?Lassoscript
		Records;
			Var:'vLast_Name'=(Field:'Last_Name');
			Var:'vFirst_Name'=(Field:'First_Name');
			Var:'vID'=(Field:'ID');
			Var:'vMail'=(Field:'Mail');
			Var:'vPost_Name1' = (Field:'Post_Name1');
			Var:'vPost_Name2' = (Field:'Post_Name2');
			Var:'vPost_Name3' = (Field:'Post_Name3');
			Var:'vPost_Name4' = (Field:'Post_Name4');
			Var:'vPost_Name5' = (Field:'Post_Name5');
			Var:'vEntry_Date' = (Field:'Entry_Date');
			Var:'vAllow'=(Field:'Allow');
?>
				<tr <?Lassoscript If: (Loop_Count) %2 == 0; 'bgcolor = "#F5F5F5"'; Else; 'bgcolor = "#FFFFFF"'; /If; ?> >
					<td valign="middle" width="170" class="tabletext_10_black">
						<strong><a href="setup_editrecord.lasso?DataType=User&ID=[Var:'vID']&New=Y">[Var:'vLast_Name'], [Var:'vFirst_Name']</a></strong></td>
					<td valign="middle" width="80" class="tabletext_10_black">
						[Var:'vMail']
					</td>
					<td valign="middle" width="250" class="tabletext_10_black">
						[Var:'vPost_Name1']
<?Lassoscript
If: $vPost_Name2 != ''; ('&nbsp;&nbsp;&nbsp;'+($vPost_Name2)); /If;
If: $vPost_Name3 != ''; ('&nbsp;&nbsp;&nbsp;'+($vPost_Name3)); /If;
If: $vPost_Name4 != ''; ('&nbsp;&nbsp;&nbsp;'+($vPost_Name4)); /If;
If: $vPost_Name5 != ''; ('&nbsp;&nbsp;&nbsp;'+($vPost_Name5)); /If;
?>					</td>
					<td valign="middle" width="80" class="tabletext_10_black">
						[Var:'vEntry_Date']
					</td>
					<td valign="middle" width="30" class="tabletext_10_black">
						[Var:'vAllow']
					</td>
				</tr>
		[/Records]
	<tr>
		<td colspan="5" valign="top" bgcolor="#cccccc">&nbsp;</td>
	</tr>
	<tr>
		<td colspan="5" valign="middle" align="left" bgcolor="#f5f5f5">
			<div align="center">[$NavControlLinks]</div>
		</td>
	</tr>
		</table>
[/Inline]
